from django.http import HttpResponse
from reportlab.platypus import SimpleDocTemplate, Table
from reportlab.lib.pagesizes import letter, landscape
from reportlab.lib.units import inch
from seeker.utils import parse_display_field

def render( request, plugin, search, result, page=0, page_size=-1 ):
	fields = [ parse_display_field(f) for f in search.display_fields ]
	headers = [ plugin.get_field_name(request,x[0]) for x in fields ]
	num_rows = result.count()
	data = []
	data.append( headers )
	for k in range(num_rows):
		row = [ result.get_value(k,x[0],x[1]) for x in fields ]
		data.append( row )
	
	response = HttpResponse( mimetype='application/pdf' )
	response['Content-disposition'] = 'attachment; filename="export.pdf"'
	
	doc = SimpleDocTemplate( response, pagesize=landscape(letter), topMargin=0.5*inch, leftMargin=0.5*inch, rightMargin=0.5*inch, bottomMargin=0.5*inch )
	t = Table( data )
	doc.build( [t] )
	
	return response
